package yns.springboot.guide.demo.data.consumer.rabbitmq.driver;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;

public class RabbitmqDriverConsumer extends DefaultConsumer {
	
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	public RabbitmqDriverConsumer(Channel channel) {
		super(channel);
	}

	@Override
	public void handleConsumeOk(String consumerTag) {
		super.handleConsumeOk(consumerTag);
		
		logger.info("handleConsumeOk consumerTag : " + consumerTag);
		
	}

	@Override
	public void handleCancelOk(String consumerTag) {
		super.handleCancelOk(consumerTag);
		
		logger.info("handleCancelOk consumerTag : " + consumerTag);
		
	}

	@Override
	public void handleCancel(String consumerTag) throws IOException {
		super.handleCancel(consumerTag);
		
		logger.info("handleCancel consumerTag : " + consumerTag);
		
	}

	@Override
	public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
			throws IOException {
		super.handleDelivery(consumerTag, envelope, properties, body);

		String message = new String(body, "UTF-8");
		
		logger.info("handleDelivery ---------------------------  ");
		logger.info("handleDelivery --  Received message : " + message );
		logger.info("handleDelivery --  ConsumerTag :   " + consumerTag);
		logger.info("handleDelivery --  Envelope :   " + envelope.toString());
		logger.info("handleDelivery --  BasicProperties :   " + properties.toString());
		logger.info("handleDelivery ---------------------------  ");
	
		
//		this.getChannel().basicAck(envelope.getDeliveryTag(), false);
	}

	@Override
	public void handleShutdownSignal(String consumerTag, ShutdownSignalException sig) {
		super.handleShutdownSignal(consumerTag, sig);
		
		logger.info("handleShutdownSignal consumerTag : " + consumerTag);
		logger.info("handleShutdownSignal ShutdownSignalException : " + sig);
	
	}

	@Override
	public void handleRecoverOk(String consumerTag) {
		super.handleRecoverOk(consumerTag);
		
		logger.info("handleRecoverOk consumerTag : " + consumerTag);
		
	}

}
